﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Master/Default.Master" AutoEventWireup="true" CodeBehind="addRemoveStudent.aspx.cs" Inherits="PointTracker.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderHead" runat="server">
    <script language="javascript" type="text/javascript">
        // Verifies that all the necessary fields for adding an individual student are filled in
        function VerifyFields() {
            var errors = false;
            var list = "";
            var divField = document.getElementById("Errors");
            divField.setAttribute("style", "color:red;");
            list += "<ul>";

            if (document.getElementById("<%= TextBoxFname.ClientID %>").value == "") {
                list += "<li>The first name field cannot be empty</li>";
                errors = true;
            }
            if (document.getElementById("<%= TextBoxLname.ClientID %>").value == "") {
                list += "<li>The last name field cannot be empty</li>";
                errors = true;
            }
            if (document.getElementById("<%= TextBoxDu_id.ClientID %>").value == "") {
                list += "<li>The Drexel ID field cannot be empty</li>";
                errors = true;
            }
            if (document.getElementById("<%= TextBoxCRN.ClientID %>").value == "") {
                list += "<li>The CRN field cannot be empty</li>";
                errors = true;
            }

            if (isNaN(document.getElementById("<%= TextBoxDu_id.ClientID %>").value)) {
                list += "<li>The CRN field has to be an integer</li>";
                errors = true;
            }
            if (isNaN(document.getElementById("<%= TextBoxCRN.ClientID %>").value)) {
                list += "<li>The CRN field has to be an integer</li>";
                errors = true;
            }

            divField.innerHTML = list;
            divField.innerHTML += "</ul>";
            return !errors;
        }

        // Verifies that the information entered into the big text area is delimited by new lines (students), and then by commas (student info)
        function VerifyBatchField() {
            var list = "<ul>";
            var errors = false;
            var divField = document.getElementById("Errors2");
            divField.setAttribute("style", "color:red;");

            if (document.getElementById("<%= TextBoxBatchCRN.ClientID %>").value == "") {
                list += "<li>The CRN field cannot be empty for batch uploads</li>";
                errors = true;
            }
            
            var isGood = true;
            var batchUploadText = document.getElementById("<%= TextBoxBatchUpload.ClientID %>").value;
            var batchUploadArray = batchUploadText.split("\n");
            
            for (var i = 0; i < batchUploadArray.length && isGood; i++) {
                var studentLineArray = batchUploadArray[i].split(",");
                if (studentLineArray.length != 3)
                    isGood = false;
            }

            if (isNaN(document.getElementById("<%= TextBoxBatchCRN.ClientID %>").value)) {
                list += "<li>The CRN field has to be an integer</li>";
                errors = true;
            }

            if (!isGood) {
                list += "<li>Incorrect format in batch upload text area</li>";
                errors = true;
            }

            divField.innerHTML = list;
            return !errors;
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderBody" runat="server">
    <fieldset>
    <legend>Add a Single Student</legend>
    <div id="Errors"></div>
    <table>
        <tr>
            <td style="width: 100px;">
                First name:
            </td>
            <td>
                <asp:TextBox ID="TextBoxFname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Last name:
            </td>
            <td>
                <asp:TextBox ID="TextBoxLname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Drexel ID:
            </td>
            <td>
                <asp:TextBox ID="TextBoxDu_id" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Class CRN:
            </td>
            <td>
                <asp:TextBox ID="TextBoxCRN" MaxLength="5" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <asp:Button ID="ButtonAddStudent" OnClick="AddSingleStudent" OnClientClick="return VerifyFields();" runat="server" Text="Add Student"/>
            </td>
        </tr>
    </table>
    </fieldset>

    <fieldset>
    <legend>Add Multiple Students</legend>
    <div id="Errors2"></div>
    <table>
        <tr>
            <td style="width: 100px;">
                Class CRN:
            </td>
            <td>
                <asp:TextBox ID="TextBoxBatchCRN" MaxLength="5" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td valign="top">
                List of Students:
            </td>
            <td>
                <asp:TextBox ID="TextBoxBatchUpload" runat="server" Rows="5" Columns="40" TextMode="MultiLine"></asp:TextBox>
            </td>
            <td rowspan="4">
                <div style="width: 200px; height: 200px;">
                    Correct batch upload format:<br /><br />
                    Lname_1, Fname_1, ID_1
                    Lname_2, Fname_2, ID_2
                    Lname_3, Fname_3, ID_3
                    <br />
                    ...
                </div>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <asp:Button ID="ButtonAddStudents" OnClick="AddBatchStudent" OnClientClick="return VerifyBatchField();" runat="server" Text="Add Students"/>
            </td>
        </tr>
    </table>
    </fieldset>
</asp:Content>
